package com.wingstudio.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import com.wingstudio.springcloud.service.OrderHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author ITcz
 * @Data 2021-04-15 - 21:38
 */
@RestController
@RequestMapping("/consumer")
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
@Slf4j
public class OrderHystrixController {

    @Autowired
    private OrderHystrixService orderHystrixService;

    @GetMapping("/payment/lock/{id}")
    public String paymentInfo_LOCK(@PathVariable("id") Integer id){
        String s = orderHystrixService.paymentInfo_OK(id);
        return s;
    }

    @GetMapping("/payment/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        String s = orderHystrixService.paymentInfo_OK(id);
        return s;
    }

    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod", commandProperties =
            {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2500")})
    @GetMapping("/payment/error/{id}")
    public String paymentInfo_ERROR(@PathVariable("id") Integer id){
        String s = orderHystrixService.paymentInfo_ERROR(id);
        return s;
    }

    protected String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id) {
        return "消费者80端口：支付系统业务繁忙请10s后再试：" + id;
    }

//    全局fallback方法
    protected String payment_Global_FallbackMethod(){
        return "GLOBAL ERROR：异常处理，请稍后再试";
    }
}
